package cs236369.hw5.axis2.client;

import java.rmi.RemoteException;
import java.util.Arrays;

import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;

import cs236369.hw5.axis2.client.SearchWSServiceStub.SearchKeywords;
import cs236369.hw5.axis2.client.SearchWSServiceStub.SearchKeywordsResponse;
import cs236369.hw5.axis2.client.SearchWSServiceStub.SearchLocal;

public class SearchWSClient
{
	
	private SearchWSServiceStub stub;
	
	public SearchWSClient(String url) throws AxisFault
	{
		this.stub = new SearchWSServiceStub(url);
	}

	/**
	 * 
	 * @param service is the service url, example:
	 *            http://ibm411.cs.technion.ac.il/HW5/services/RegistrationService
	 * @return 
	 */
	public String[] searchKeywords(String[] keywords)
	{
		Logger logger = Logger.getLogger(getClass());
		
		try
		{
			SearchKeywords searchKeywords = new SearchKeywords();
			searchKeywords.setKeywords(keywords);
			SearchKeywordsResponse response = stub.searchKeywords(searchKeywords);
			String[] res = response.getSearchKeywordsReturn();
			logger.debug("response is: " + Arrays.toString(res));
			return res;
//			return new String[0];
		}
		catch (AxisFault e)
		{
			logger.error("Axis exception", e);
		}
		catch (RemoteException e)
		{
			logger.error("Remote exception", e);
		}
		return null;

	}

	
	public String[] searchLocal(double locationX, double locationY, double d)
	{
		Logger logger = Logger.getLogger(getClass());
		
		try
		{

			SearchLocal searchLocal = new SearchLocal();			
			searchLocal.setLocationX(locationX);
			searchLocal.setLocationY(locationY);
			searchLocal.setD(d);
			String[] res = stub.searchLocal(searchLocal).getSearchLocalReturn();
			logger.debug("response is: " + Arrays.toString(res));
			return res;
			
		}
		catch (AxisFault e)
		{
			logger.error("Axis exception", e);
		}
		catch (RemoteException e)
		{
			logger.error("Remote exception", e);
		}
		return null;	
	}

}
